package com.redlight.token.security;

import com.redlight.exception.Exceptions;
import com.redlight.utils.Encodes;
import org.apache.commons.lang3.Validate;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @author chao
 * @Title: Digests
 * @date 2019-01-24 21:49
 */
public class Digests {
    public static final String SHA1 = "SHA-1";
    public static final String MD5 = "MD5";
    public static final int HASH_INTERATIONS = 1024;
    public static final int SALT_SIZE = 8;
    private static SecureRandom random = new SecureRandom();

    public Digests() {
    }

    private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
        try {
            MessageDigest digest = MessageDigest.getInstance(algorithm);
            if (salt != null) {
                digest.update(salt);
            }

            byte[] result = digest.digest(input);

            for(int i = 1; i < iterations; ++i) {
                digest.reset();
                result = digest.digest(result);
            }

            return result;
        } catch (GeneralSecurityException var7) {
            throw Exceptions.unchecked(var7);
        }
    }

    public static byte[] generateSalt(int numBytes) {
        Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", (long)numBytes);
        byte[] bytes = new byte[numBytes];
        random.nextBytes(bytes);
        return bytes;
    }

    private static byte[] digest(InputStream input, String algorithm) throws IOException {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
            int bufferLength = 8192;
            byte[] buffer = new byte[bufferLength];

            for(int read = input.read(buffer, 0, bufferLength); read > -1; read = input.read(buffer, 0, bufferLength)) {
                messageDigest.update(buffer, 0, read);
            }

            return messageDigest.digest();
        } catch (GeneralSecurityException var6) {
            throw Exceptions.unchecked(var6);
        }
    }

    public static String entryptPassword(String plainPassword) {
        byte[] salt = generateSalt(8);
        byte[] plainPasswordHex = digest(plainPassword.getBytes(), "SHA-1", salt, 1024);
        String dbPwd = Encodes.encodeHex(salt) + Encodes.encodeBase64(plainPasswordHex) + randomStr(plainPassword.length());
        return dbPwd;
    }

    public static boolean validatePassword(String plainPassword, String dbPassword) {
        try {
            byte[] salt = Encodes.decodeHex(dbPassword.substring(0, 16));
            byte[] plainPwdDigest = digest(plainPassword.getBytes(), "SHA-1", salt, 1024);
            String realPassword = dbPassword.substring(16, dbPassword.length() - plainPassword.length());
            return Encodes.encodeBase64(plainPwdDigest).equals(realPassword);
        } catch (Exception var5) {
            return false;
        }
    }

    public static String randomStr(int len) {
        StringBuffer ret = new StringBuffer();
        Random r = new Random();

        while(ret.length() < len) {
            ret.append(r.nextInt(10));
        }

        return ret.toString();
    }
}
